package com.tanhua.server.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.server.api.LingHunApi;
//import com.tanhua.server.pojo.LingHunCun;
import com.tanhua.dubbo.server.pojo.LingHunCun;
import com.tanhua.server.pojo.User;
import com.tanhua.server.pojo.UserInfo;
import com.tanhua.server.utils.UserThreadLocal;
import com.tanhua.server.vo.LingHunQv;
import com.tanhua.server.vo.PicUploadResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class LingHunService {


//    @Autowired
//    private UserService userService;

    @Autowired
    private UserInfoService userInfoService;

    @Reference(version = "1.0.0")
    private LingHunApi lingHunApi;

    @Autowired
    private PicUploadService picUploadService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

//    @Autowired
//    private MongoTemplate mongoTemplate;


    public Boolean save(MultipartFile file, String token) {
        //校验token
//        User user = this.userService.queryUserByToken(token);
//        if (null == user) {
//            return false;
//        }


        //语音上传到阿里云OSS
        PicUploadResult result = this.picUploadService.upload(file);
        if (StringUtils.isEmpty(result.getName())) {
            //上传失败
            return false;
        }

        //把语音保存到灵魂表中
        LingHunCun lingHunCun = new LingHunCun();
        lingHunCun.setSoundUrl(result.getName());//存语音地址
        //获取用户id
        User user=UserThreadLocal.get();
        //Long id = user.getId();
        lingHunCun.setUserid(Math.toIntExact(user.getId()));//存用户id

        UserInfo userInfo = this.userInfoService.queryUserInfoById(user.getId());
        lingHunCun.setAvatar(userInfo.getLogo());
        lingHunCun.setNickname(userInfo.getNickName());
        lingHunCun.setGender(userInfo.getSex().getValue()==1?"man":"woman");
        lingHunCun.setAge(userInfo.getAge());

        //System.out.println(lingHunCun);

        // boolean b = this.lingHunApi.saveLingHunCun(lingHunCun);

        return this.lingHunApi.saveLingHunCun(lingHunCun);


    }

    public LingHunQv find(){

        //获取用户id
        User user=UserThreadLocal.get();
        Long userId = user.getId();
        //String cunId=userId+"HUNAN";

        String s = this.redisTemplate.opsForValue().get(userId + "");

        if (s==null){
            //存缓存24小时
            this.redisTemplate.opsForValue().set(userId+"", "10",24, TimeUnit.HOURS);
        }else {
            if (Integer.valueOf(s)<1){
                return null;
            }

        }

        List lingHunList = this.lingHunApi.lingHunList(userId);

        if (lingHunList==null){
            return null;
        }

        //打乱集合数据
        Collections.shuffle(lingHunList);
        //取第一个
        LingHunCun lingHunCun= (LingHunCun) lingHunList.get(0);


        LingHunQv lingHunQv = new LingHunQv();


        lingHunQv.setId(lingHunCun.getUserid());
        lingHunQv.setAvatar(lingHunCun.getAvatar());
        lingHunQv.setNickname(lingHunCun.getNickname());
        lingHunQv.setGender(lingHunCun.getGender());
        lingHunQv.setAge(lingHunCun.getAge());
        lingHunQv.setSoundUrl(lingHunCun.getSoundUrl());
        lingHunQv.setRemainingTimes(Integer.valueOf(s));



        //减一
        this.redisTemplate.opsForValue().decrement(userId+"");
        return  lingHunQv;


    }

}